package niumao.minecraft.uhc;

import niumao.minecraft.uhc.recipes.UHCItem;
import niumao.minecraft.uhc.recipes.recipeGui;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import java.util.ArrayList;
import java.util.Random;

public class LuckyBlockGame implements Listener {
    private static final Main plugin = Main.getPlugin(Main.class);
    private static World luckWorld;
    private static final ArrayList<ItemStack> luckyLoot = new ArrayList<>();

    public static void start() {
        WorldCreator worldCreator = new WorldCreator("luckyblock");
        luckWorld = Bukkit.createWorld(worldCreator);

        UHCItem item = new UHCItem();
        luckyLoot.add(item.ApprenticeHelmet());
        luckyLoot.add(item.ApprenticeChestPlate());
        luckyLoot.add(item.ApprenticeLeggings());
        luckyLoot.add(item.ApprenticeBoots());
        luckyLoot.add(item.ApprenticeSword());
        luckyLoot.add(item.ApprenticeBow());
        luckyLoot.add(item.MasterCompass());
        luckyLoot.add(item.VorpalSword());
        luckyLoot.add(item.Sharpness());
        luckyLoot.add(item.DragonSword());
        luckyLoot.add(item.Protection());
        luckyLoot.add(item.Projectile_Protection());
        luckyLoot.add(item.DragonArmor());
        luckyLoot.add(item.SpikedArmor());
        luckyLoot.add(item.SevenLeagueBoots());
        luckyLoot.add(item.TarnHelm());
        luckyLoot.add(item.PhilosopherPickAxe());
        luckyLoot.add(item.Velocity());
        luckyLoot.add(item.IronPickaxe());
        luckyLoot.add(item.LumberAxe());
        luckyLoot.add(item.FlaskofIchor());
        luckyLoot.add(item.Exodus());
        luckyLoot.add(item.Leviathan());
        luckyLoot.add(item.Perun());
        luckyLoot.add(item.Excalibur());
        luckyLoot.add(item.Anduril());
        luckyLoot.add(item.Scythe());
        luckyLoot.add(item.Machina());
        luckyLoot.add(item.KingofRod());
        luckyLoot.add(item.Vidar());
        luckyLoot.add(item.Vitality());
        luckyLoot.add(item.Miner());
        luckyLoot.add(item.Bloodlust());
        luckyLoot.add(item.Modular());
        luckyLoot.add(item.Hermes());
        luckyLoot.add(item.Barbarian());
        luckyLoot.add(item.Cupid());
        luckyLoot.add(item.Artemis());
        luckyLoot.add(item.GoldenHead());
        luckyLoot.add(item.Power());
        luckyLoot.add(item.FusionArmor());
        luckyLoot.add(item.Nectar());
        luckyLoot.add(item.Cornucopia());
        luckyLoot.add(item.LeatherEconomy());
        luckyLoot.add(item.DustOfLight());
        luckyLoot.add(item.BrewingArtifact());
        luckyLoot.add(item.DeliciousSteak());
        luckyLoot.add(item.DeliciousPorkChop());
        luckyLoot.add(item.DeliciousMutton());
        luckyLoot.add(item.DeliciousChicken());
        luckyLoot.add(item.DeliciousRabbit());
        luckyLoot.add(item.DeliciousCod());
        luckyLoot.add(item.DeliciousSalmon());
        luckyLoot.add(item.FlamingArtifact());
        luckyLoot.add(item.IronPack());
        luckyLoot.add(item.Obsidian());
        luckyLoot.add(item.EnlighteningPack());
        luckyLoot.add(item.LightAnvil());
        luckyLoot.add(item.LightEnchantmentTable());
        luckyLoot.add(item.Temptation());
        luckyLoot.add(item.LightApple());
        luckyLoot.add(item.LightEnchantingApple());
        luckyLoot.add(item.ArrowEconomy());
        luckyLoot.add(item.Saddle());
        luckyLoot.add(item.GoldPack());
        luckyLoot.add(item.SugarRush());
        luckyLoot.add(item.Elytra());
        luckyLoot.add(new ItemStack(Material.ARROW, 64));
        luckyLoot.add(new ItemStack(Material.ARROW, 64));
        luckyLoot.add(new ItemStack(Material.ARROW, 64));

        for (Player player : Bukkit.getOnlinePlayers()) {
            player.teleport(luckWorld.getSpawnLocation());
            player.setGameMode(GameMode.SURVIVAL);
            player.getInventory().addItem(new ItemStack(Material.COOKED_BEEF, 64), new ItemStack(Material.IRON_PICKAXE));
            player.setInvulnerable(false);
            player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 999999 * 20, 0), true);
        }
        luckWorld.setPVP(false);
        luckWorld.getWorldBorder().setSize(100);
        luckWorld.setDifficulty(Difficulty.HARD);
    }

    public static void over() {
        for (Player player : Bukkit.getOnlinePlayers()) {
            for (int i = 0; i <= 40; i++) {
                ItemStack item = player.getInventory().getItem(i);
                if (item == null) continue;
                player.getInventory().setItem(i, null);
                player.getInventory().setItem(0, recipeGui.RecipeBook());
            }
            player.teleport(Bukkit.getWorld("world").getSpawnLocation());
        }
        Bukkit.unloadWorld(luckWorld, false);
    }

    @EventHandler
    public void onBreakLuckyBlock(BlockBreakEvent event) {
        if (!event.getPlayer().getWorld().getName().equalsIgnoreCase("luckyblock")) return;
        Block block = event.getBlock();
        if (block.getType() == Material.GOLD_BLOCK) {
            event.setDropItems(false);
            World world = event.getPlayer().getWorld();
            world.dropItem(block.getLocation(), luckyLoot.get(new Random().nextInt(luckyLoot.size())));
        }
    }
}
